package com.djsearch.ui;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Text;

import com.djsearch.R;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.LvYouGoodsListBean;
import com.djsearch.bean.Movie;
import com.djsearch.bean.TravelBean;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.djsearch.data.ImageManager;
import com.djsearch.util.AsyncImageLoader;
import com.djsearch.util.AsyncImageLoader.ImageCallback;
import com.djsearch.widget.WheelMain;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 旅游详情页面
 * 
 * @author chenyang
 * 
 */
public class LvYouDetailsActivity extends BaseActivity {

	private Context mContext;
	private Button backBtn;
	private LinearLayout shouChangLayout, shareLayout, taoGouLayout;// 收藏 分享 淘购
	private TravelBean traveBean;
	private Button lookBtn, locationBtn,buyBtn;// 去看看按钮,定位按钮
	private RelativeLayout locationLayout;
	private PopupWindow popuWindow;
	private TextView locationInfoText;
	private LvYouGoodsListBean lvYouGoodsListBean;
	private AsyncImageLoader asyncImageLoader;
	private ProgressBar progressBar;
	private ImageView goodsImg;
	private TextView describeText,benZhan,canKao,zuiHui,phoneNumText,daiJin;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.lvyou_details);
		mContext = this;
		lvYouGoodsListBean = (LvYouGoodsListBean) getIntent().getSerializableExtra("data");
		asyncImageLoader = new AsyncImageLoader("bigPic");
		// 初始化主界面
		initView();
		// 初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub

	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		backBtn = (Button) findViewById(R.id.btnBack);
		progressBar = (ProgressBar)findViewById(R.id.progressBar);
		shouChangLayout = (LinearLayout) findViewById(R.id.shouChangLayout);
		shareLayout = (LinearLayout) findViewById(R.id.shareLayout);
		taoGouLayout = (LinearLayout) findViewById(R.id.taoGouLayout);
		locationBtn = (Button) findViewById(R.id.btndingwei);
		locationLayout = (RelativeLayout)findViewById(R.id.locationLayout);
		locationInfoText = (TextView)findViewById(R.id.locationInfoText);
		buyBtn = (Button)findViewById(R.id.buyBtn);
		goodsImg = (ImageView)findViewById(R.id.goodsImg);
        asyncImageLoader.loadDrawable(progressBar,goodsImg,"http://www.cnlxs.org/images/" + lvYouGoodsListBean.getSale_DataImage(), new ImageCallback() {
			
			@Override
			public void imageLoaded(ProgressBar progressBar,ImageView imageView,Drawable imageDrawable, String imageUrl, String tag) {
				// TODO Auto-generated method stub
				goodsImg.setImageBitmap(ImageManager.drawableToBitmap(imageDrawable,464.0f));
				progressBar.setVisibility(View.GONE);
			}
		});
        describeText = (TextView)findViewById(R.id.describe);
        describeText.setText(lvYouGoodsListBean.getSale_DataCName());
        benZhan = (TextView)findViewById(R.id.benZhan);
        benZhan.setText(lvYouGoodsListBean.getSale_DataTwo());
        canKao = (TextView)findViewById(R.id.canKao);
        canKao.setText(String.valueOf(Float.parseFloat(lvYouGoodsListBean.getSale_DataTwo())*1.2));
        zuiHui = (TextView)findViewById(R.id.zuiHui);
        zuiHui.setText(String.valueOf(Float.parseFloat(lvYouGoodsListBean.getSale_DataTwo())*0.2));
        phoneNumText = (TextView)findViewById(R.id.phoneNumText);
        daiJin = (TextView)findViewById(R.id.daiJin);
        daiJin.setText(lvYouGoodsListBean.getDaiJinQuanDi());

        
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub
		backBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(mContext, LvYouActivity.class, 1);
			}
		});
		
		
		phoneNumText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Intent intent = new Intent(Intent.ACTION_CALL);   
//				intent.setData(Uri.parse("4000837624"));   
//				startActivity(intent);  
			}
		});
		
		shouChangLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isLogin()) {
					Toast.makeText(mContext, "未登录，不能收藏", 5000).show();
				} else {
					// 开始收藏
					if (traveBean != null) {
						requestShouChang(initRequestMap(traveBean));
					}
				}
			}
		});
		shareLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 分享
				Intent intent = new Intent(mContext, ShareToWeiboActivity.class);
				Bundle bundle = new Bundle();
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}
		});
		taoGouLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(mContext, TaoGouActivity.class, 0);
			}
		});

		locationBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				double locInfo[] = new double[2];
				locInfo[0] = Double.parseDouble(traveBean.getLatitude());
				locInfo[1] = Double.parseDouble(traveBean.getLongitude());
				Intent intent = new Intent(mContext, LocInfoActivity.class);
				Bundle bundle = new Bundle();
				bundle.putDoubleArray("locinfo", locInfo);
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}
		});
		
		
		buyBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (locationInfoText.getText().toString().equals("")) {
					startAnimation(locationLayout);
				} else {
					startActivity(mContext, SureOrderActivity.class, 0);
				}
				
			}
		});
		
		/**
		 * 选择时间
		 */
		locationLayout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDateTimePicker(v);
			}
		});

	}

	private Map<String, String> initRequestMap(TravelBean travelBean) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", preferences.getString("userId", ""));// 用户id

		return map;
	}

	private void requestShouChang(Map<String, String> map) {

		IRequest request = new JSONReq("collect", map);
		HTTP.post(request, new AsyncHandler() {
			@Override
			public void onStart() {
				showDialog("正在收藏...");
			}

			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if (IResponse.Success == result) {
					getDataToResolve(response);
				} else if (IResponse.Fail == result) {
					Toast.makeText(mContext, "收藏失败", 5000).show();
				}
				missDialog();
			}

			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				// super.onFailure(e, response);
				missDialog();
				// Log.e("GROUP", response);
				Toast.makeText(mContext, response, 5000).show();
				// missProgressBar();
			}

		});

	}

	
	
	
	/**
	 * 点击返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(mContext, LvYouActivity.class, 1);
		}
		return false;
	}

	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub

	}
	
	
	
	
	private void showDateTimePicker(View v) {

		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_layout, null);
		View viewTwo = inflater.inflate(R.layout.time_popu_layout, null);
		popuWindow = new PopupWindow(  
				viewTwo, getWidth(), 2*getHeight()/5-50);
		final WheelMain main = new WheelMain(popuWindow, view);
		main.showDateTimePicker(v);
		Button btn_sure = (Button) view.findViewById(R.id.btn_datetime_sure);
		btn_sure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				popuWindow.dismiss();
				String time = main.getTime();
				String str[] = time.split("-");
				if (str[1].length() == 1) {
					str[1] = "0" + str[1];
				}
				if (str[2].length() == 1) {
					str[2] = "0" + str[2];
				}
				String newTime = str[0] + "-" + str[1] + "-" + str[2];
				locationInfoText.setText(newTime.toString());
//				System.out.println(main.getTime());
			}
		});
	}
	
}
